define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            $(".btn-add").data('area',["100%",'100%']);

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coll/disparity/index' + location.search,
                    add_url: 'coll/disparity/add',
                    // edit_url: 'coll/disparity/edit',
                    // del_url: 'coll/disparity/del',
                    multi_url: 'coll/disparity/multi',
                    table: 'analysis_pay',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rowStyle:function(row, index){
                    if (row.ystatus == 3) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'did'},
                        {field: 'type', title: __('单据类型'),searchList: {1:__('采购'),2:__('销售')},},
                        {field: 'source_type', title: __('单据来源'),searchList: {2:__('采购入库单'),7:__('销售出库单'),12:__('厂家直发')}},
                        {field: 'number', title: __('编号')},
                        {field: 'source_number', title: __('关联编号')},
                        // {field: 'name', title: __('Name')},
                        {field: 'actual_cost', title: __('产生费用'), operate:false},
                        {field: 'company', title: __('客户')},
                        {field: 'nickname', title: __('供应商')},
                        // {field: 'name', title: __('Name')},
                        {field: 'explain', title: __('说明'), operate:false},
                        {field: 'status', title: __('Status'), operate:false },
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'finish',
                                    title: __('单据审核'),
                                    classname: 'btn btn-xs btn-danger  btn-click finish',
                                    icon: 'fa fa-random',
                                    // font-size:80,
                                    hidden:function(row){
                                        if(row.ystatus != 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var did = $(this).parents('tr').find('.did').html()
                                        var url = Config.this_url+'/coll/disparity/detail?ids='+did
                                        // var sid =$(this).parents('tr').find('.sid').html()
                                        // var url = Config.this_url+'/sales/slip/detail?ids='+sid
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "单据详情",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })

                                    }},
                                {
                                        name: 'share',
                                        title: __('分摊'),
                                        classname: 'btn btn-xs btn-error btn-click superpowers',
                                        icon: 'fa fa-superpowers',
                                        hidden:function(row){
                                            if(row.ystatus != 2 && row.ytype  != 1){
                                                return true;
                                            }
                                        },
                                        click: function(data){
                                            var did = $(this).parents('tr').find('.did').html()
                                            var url = Config.this_url+'/coll/disparity/share?ids='+did
                                            layer.open({
                                                type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                                area: ['100%', '100%'],
                                                title: "单据详情",
                                                content: url,
                                                // content: Config.this_url+'/purchase/requisition',
                                            })
                                        }},
                            // {
                            //         name: 'finish',
                            //         title: __('完结'),
                            //         classname: 'btn btn-xs btn-error btn-click editstatus',
                            //         icon: 'fa fa-check',
                            //         hidden:function(row){
                            //             if(row.ystatus > 1){
                            //                 return true;
                            //             }
                            //         },
                            //         click: function(data){
                            //             var did = $(this).parents('tr').find('.did').html()
                            //             layer.confirm('是否通过该单？', {
                            //                 btn: ['确定','驳回'] //按钮
                            //             }, function(){
                            //                 var url = Config.this_url+'/coll/disparity/finish?ids='+did
                            //                 $.post(url,{},function (res) {
                            //                     layer.msg(res.msg)
                            //                     if(res.code == 1){
                            //                         $('.btn-refresh').click()
                            //                     }
                            //                 })
                            //             })
                            //         }},
                                {
                                    name: 'edits',
                                    title: __('修改'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-pencil',
                                    hidden:function(row){
                                        if(row.ystatus > 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var did = $(this).parents('tr').find('.did').html()
                                        var url = Config.this_url+'/coll/disparity/edit?ids='+did
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['100%', '100%'],
                                            title: "修改",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true)
                                // if(row.status > 1){
                                    // $(table).data("operate-edit", null);
                                // }
                                // $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });
            // $('.btn-adds').click(function () {
            //     var url = Config.this_url+'/coll/disparity/adds'
            //     layer.open({
            //         type: 2, // 1-在content中写html代码， 2-在content中写src路径
            //         area: ['80%', '90%'],
            //         title: "厂家直发",
            //         content: url,
            //         // content: Config.this_url+'/purchase/requisition',
            //     })
            // })
            $('table').on('dblclick','td',function(){
                if($(this).parents('tr').find('.btn-editone').length >0){
                    return  false;
                }
                var did = $(this).parents('tr').find('.did').html()
                var url = Config.this_url+'/coll/disparity/detail?ids='+did
                    layer.open({
                        type: 2, // 1-在content中写html代码， 2-在content中写src路径
                        area: ['100%', '100%'],
                        title: "单据详情",
                        content: url,
                        // content: Config.this_url+'/purchase/requisition',
                    })
                // var url = Config.this_url+'/purchase/order/detail?ids='+oid
            });
            // table.on('post-body.bs.table',function () {
            //     $(".btn-editone").data('area',["100%",'100%']);
            // })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(".btn-add").click(function () {
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['70%', '90%'],
                    title: "选择订单",
                    content: 'seach',
                })
            })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        share: function () {
            $('.btn-share').click(function () {
                var did = $(this).parents('tr').find('.gid').val()
                var url = Config.this_url+'/coll/disparity/sharegoods'+location.search
                var chajia = $(this).parents('tr').find('.price_diff').html()*1
                var oldbalance = $(this).parents('tr').find('.balance').html()*1
                var stock = $(this).parents('tr').find('.stock').html()*1
                newbalance = oldbalance+chajia
                newbalance = newbalance.toFixed(4)
                newcost = newbalance/stock
                newcost = newcost.toFixed(4)
                layer.confirm('是否将差价'+chajia+'划入？<br>预计平摊后成本为'+newbalance+'<br>成本为'+newcost, {
                    title:'警告',
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.post(url,{did:did},function (res) {
                        layer.msg(res.msg)
                        if(res.code==1){
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                        }
                    })
                })

            })
            Controller.api.bindevent();
        },
        detail: function () {
            $('.cancelbill').click(function () {
                var url =  Config.this_url+'/coll/disparity/cancelbill'+location.search
                layer.prompt({title: '输入红冲原因', formType: 2}, function(pass, index){
                    if(pass == ''){
                        layer.msg('不能为空');
                        return false;
                    }
                    $.post(url,{reason:pass},function (res) {
                        layer.msg(res.msg)
                        if(res.code == 1){
                            layer.close(index);
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                            window.parent.$(".toolbar .btn-refresh").trigger('click');
                        }
                    })
                });
            })
            Controller.api.bindevent();
        },
        seach:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coll/disparity/seach',
                    table: '',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize:15,
                commonSearch: false,
                visible: false,
                showToggle: false,
                showColumns: false,
                // search:false,
                showExport: false,
                searchFormVisible: true,
                // searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        // {checkbox: true,operate:false},
                        {field: 'id', title: 'ID', operate: false,class:'sid'},
                        // {field: 'title', title: __('title'),  operate: false},
                        // {field: 'place_origin', title: __('place_origin'), formatter: Table.api.formatter.search},
                        {field: 'number', title: __('单据编号'),class: 'number'},
                        {field: 'type', title: __('type'),operate: false, align: 'left'},
                        // {field: 'specs', title: __('specs')},
                        // {field: 'stock', title: __('stock')},
                        {field: 'createtime', title: __('Create time'),operate: false, formatter: Table.api.formatter.datetime,  addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('选中'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-fighter-jet',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var number =$(this).parents('tr').find('.number').html()
                                        $(this).attr('style','display:none')
                                        parent.getrequisition(number);
                                    }}
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var sid =$(this).parents('tr').find('.sid').html()
                parent.getrequisition(sid);
                var index = parent.Layer.getFrameIndex(window.name);
                parent.Layer.close(index);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});